<?php
namespace app\wxpro\controller;
use app\service\imageService;
use app\BaseController;
use think\facade\Db;
use think\Log;
class Applet extends BaseController{
	public function index(){
        $res=(new imageService())->createwxuserimg(1);
        var_dump($res);
	}

	//微信公众号绑定用户
	public function userBind(){
        $openid = request()->post("openid");
        $phone = request()->post("phone");
        $wxuserinfo = Db::table("wxuser")->where("bind_code",$phone)->find();
        if(empty($wxuserinfo)){
            return json_encode(["errCode"=>"1",'errormsg' => '绑定码不正确']);
        }
        if(!empty($wxuserinfo["openid"])){
            return json_encode(["errCode"=>"1",'errormsg' => '该绑定码已经绑定']);
        }
        $res = Db::table("wxuser")->where("id",$wxuserinfo["id"])->update(["openid"=>$openid,"update_time"=>time()]);
        if($res){
            return json_encode(["errCode"=>"0",'msg' => '绑定成功']);
        }else{
            return json_encode(["errCode"=>"1",'errormsg' => '绑定失败']);
        }
    }
	//查询访客是否能访问二维码
	public function visitorStatus(){
        return json_encode(["errCode"=>"1",'errormsg' => '用户信息获取失败']);
    }
    //查询是否是业主，租户，家人
    public function userStatus(){
	    $openid = request()->param("openid");
	    if(empty($openid)){
            return json_encode(["errCode"=>"99",'errormsg' => '用户信息获取失败']);
        }
        $wxuserinfo = Db::table("wxuser")->where("openid",$openid)->find();
	    if(empty($wxuserinfo)){
            return json_encode(["errCode"=>"1",'errormsg' => '你未绑定房屋，无权访问']);
        }else{
            return json_encode(["errCode"=>0]);
        }

    }

    public function userInfo(){
        $openid= request()->param("openid");
        if(empty($openid)){
            return json_encode(["errCode"=>"1",'errormsg' => '用户信息获取失败']);
        }
        $wxuserinfo = Db::table("wxuser")
            ->where("openid",$openid)
            ->find();
        if(empty($wxuserinfo)){
            return json_encode(["errCode"=>"1",'errormsg' => '无权访问']);
        }
        if(empty($wxuserinfo["ewm"])){

            $resewm=(new imageService())->createwxuserimg($wxuserinfo["id"]);
            if($resewm)
            {
                $ewm="https://www.zz.zfbzhsq.com/static/ewm/wxuser/".$wxuserinfo["id"].".png";
                $wxuserinfo["ewm"] = $ewm;
                DB::table("wxuser")->where("id",$wxuserinfo["id"])->update(["ewm"=>$ewm]);
            }else
            {
                return json(["errCode"=>"1",'errormsg' => "二维码生成失败"]);
            }
        }
        $data["wxuserinfo"]=  $wxuserinfo;
        return json_encode(["errCode"=>"0",'datainfo' =>$data]);
    }

    //查询小区电话
    public function communityPhone(){
        $openid = request()->param("openid");
        if(empty($openid)){
            return json_encode(["errCode"=>"1",'errormsg' => '用户信息获取失败']);
        }
        $wxuserinfo = Db::table("wxuser")->where("openid",$openid)->find();
        if(empty($wxuserinfo)){
            return json_encode(["errCode"=>"1",'errormsg' => '无权访问']);
        }
        $list = Db::table("community_contract")->where("community_id",$wxuserinfo["community_id"])->select();
        return json_encode(["errCode"=>"0",'list' => $list]);
    }
    public function openid(){
		$code = request()->get('code');
        $appid = "wxe0675effc8906167";//小程序的唯一标识
        $secret = "c425b9a0a77cf56e4654d3e56ce8b1e2";//小程序的AppSecret
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret=' . $secret . '&js_code=' . $code . '&grant_type=authorization_code';
		$curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);//要访问的地址
        curl_setopt($curl, CURLOPT_HEADER, 1);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//跳过ssl检查项
        curl_setopt($curl, CURLOPT_HEADER, 0);//显示返回的Header区域内容
        $tmpInfo = curl_exec($curl);//执行操作
        // $err = curl_error($curl);//输出错误
        curl_close($curl);//关闭CURL会话
        // $tmpInfo = json_decode(json_encode(simplexml_load_string($tmpInfo, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        $tmpInfo = $this->object_to_array(json_decode($tmpInfo));
		//Log::write($tmpInfo,"用户信息");
        if (!empty($tmpInfo['errmsg'])) {
			return json(['status' => '0',"error"=>$tmpInfo['errmsg']]);
            return $tmpInfo['errmsg'];
        } else {
			return json(['status' => '1',"openid"=>$tmpInfo['openid']]);
            $user['user_id'] = $tmpInfo['openid'];
            return json_encode($user['user_id']);
        }
    }
	public function formIdAdd(){
		$wxpro_form["ctime"]=time();
		$wxpro_form["openid"]=request()->get("openid");
		$wxpro_form["form_id"]=request()->get("formId");
		$wxpro_form["num"]=1;
		$res=Db::table("wxpro_form")->insert($wxpro_form);
	}
	
    public function object_to_array($obj){
        $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
        foreach($_arr as $key=>$val){
            if($key !== 'gioouts'){
                $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;
                $arr[$key] = $val;
            }
        }
        return $arr;
    }
    
    
}